package com.xiaoma.tpo.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.xiaoma.tpo.baseDao.BaseDaoImpl;
import com.xiaoma.tpo.baseDao.DefaultRowMapper;
import com.xiaoma.tpo.domain.ForumTopic;
import com.xiaoma.tpo.domain.ForumTopicStore;
import com.xiaoma.tpo.dto.ForumTopicVo;

@Repository
public class ForumTopicStoreDao extends BaseDaoImpl<ForumTopicStore>{

    @SuppressWarnings("unchecked")
    public List<ForumTopicVo> findStoredTopics(int userId) {
        String sql = "select b.id, b.user_id, IFNULL(c.nickname,c.name) user_name, c.avatar, b.title, b.content, b.created_at, b.top, b.post_count, b.imgs, b.type, b.read_count, b.share_count, b.store_count from forum_topic_store a left join forum_topic b on a.topic_id = b.id left join user c on c.id = b.user_id where a.user_id = ?";
        List<?> result = jdbcTemplate.query(sql, new DefaultRowMapper(ForumTopicVo.class, getNameHandler()), userId);
        return (List<ForumTopicVo>)result;
    }
    
    public int deleteStore(int userId, int topicId) {
        String sql = "delete from forum_topic_store where user_id = ? and topic_id = ?";
        return jdbcTemplate.update(sql, userId, topicId);
    }
}